package cn.donting.jme.edit.ui.inspector.imp.base;

import cn.donting.jme.edit.ex.ExResourcesLoad;
import cn.donting.jme.edit.ex.IconResourcesLoader;
import cn.donting.jme.edit.ui.scenetree.SceneTreeNodeView;
import cn.donting.jme.edit.ui.scenetree.SpatialTreeItem;
import cn.donting.jme.edit.util.IconUtils;
import com.jme3.math.Transform;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BaseInfoController {

    @FXML
    public TextField name;
    @FXML
    public Label type;
    @FXML
    public ImageView icon;

    private Spatial spatial;


    public void setSpatial(Spatial spatial) {
        this.spatial = spatial;
        IconResourcesLoader firstResources = ExResourcesLoad.getFirstResources(IconResourcesLoader.class);
        icon.setImage(firstResources.getIconImage(spatial));
        update();
    }

    public void update() {
        name.setText(spatial.getName());
        type.setText(spatial.getClass().getName());
        name.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                spatial.setName(newValue);
                SpatialTreeItem selectedItem = (SpatialTreeItem)SceneTreeNodeView.getInstance().treeView.getSelectionModel().getSelectedItem();
                selectedItem.setValue(spatial.toString());
            }
        });
    }

}
